package com.bluefox.uac.security;

import com.bluefox.common.web.exception.AccessDenyException;
import com.bluefox.common.web.support.CurrentUserFactoryBean;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @version v1.0
 * @ProjectName: spring-demo
 * @ClassName: CurrentUserFactoryBeanImpl
 * @Description: TODO(一句话描述该类的功能)
 * @Author: SunFeng
 * @Date: 2019/11/22 21:21
 */
public class CurrentUserFactoryBeanImpl implements CurrentUserFactoryBean {
    @Override
    public Object getCurrentUser() {
        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if ((authentication != null) && (authentication instanceof CustomAuthenticationToken)) {
            return ((CustomAuthenticationToken) authentication).getDetails();
        } else {
            throw new AccessDenyException("auth.accessDeny", "无访问权限");
        }
    }
}
